<?php
class Gm_uploadFiles {
    function uploadFiles($data, $game, $activityNameZh = "") {
        if(empty($data)) {
            return array(false, 1000);  // $_FILES 不存在
        }

        $fileName 	= $data['name'];
        $size 		= $data['size'];
        $type 		= $data['type'];
        $tmp_name 	= $data['tmp_name'];

        // 请选择活动 后上传
        if(empty($activityNameZh)) {
            return array(false, 1001);  // 尚未选择活动
        }

        /**
         * 根据中文活动名称 获取配置信息
         */

        $activityInfo = MooObj::get('ActivityConf')->getActivityInfoByZh($game, $activityNameZh);

        $excels = $activityInfo['excel'];
        $activityNameEn = $activityInfo['activityNameEn'];

        // 指定文件名称 验证
        if(!empty($excels)) {
            if(!in_array($fileName, $excels)) {
                return array(false, 1002); // 上传的文件名不支持
            }
        }

        // 文件类型验证
        $typeArr = array(
            'excel'
        );

        $node_type = $this->getFileExt($type);

        if(!in_array($node_type, $typeArr)) {
            return array(false, 1003); // 上传的文件类型不支持,请上传 .xls表格
        }

        $moveDir = ROOT_PATH . "/activityFiles/excel/{$game}/";
        $path = $moveDir . $fileName;
        if(!MooFile::isExists($moveDir)) {
            MooFile::mkDir($moveDir);
        }

        $moveRs = MooUpload::moveUploadFile($tmp_name, $path);

        $activityNameEn = $activityInfo['activityNameEn'];
        $activityId 	= $activityInfo['activityId'];

        // 上传备份
        $date = date('Y-m-d_H_i_s');
        $bakDir = $moveDir . "bak/{$activityNameEn}_{$activityId}/";
        if(!MooFile::isExists($bakDir)) {
            MooFile::mkDir($bakDir);
        }
        $bakPath = $bakDir . $date . "." . $fileName;
        MooFile::cp($path, $bakPath);

        if(!$moveRs) {
            return array(false, 1004); // 移动文件失败
        }

        // 生成xml文件
        $createXmlRes = MooObj::get('ActivityConf')->createXmlByExcel($path, $activityInfo, $fileName, 0);
        if(!$createXmlRes[0]) {
            return array(false, 1007); // xml生成失败
        }

        // 生成xml路径 记录数据库
        $xmlPath = $createXmlRes[1];

        // 生成lua文件
        $createLuaRes = MooObj::get('ActivityConf')->createLuaByExcel($path, $activityInfo, $fileName, 0);
        if(!$createLuaRes[0]) {
            return array(false, 1008); // lua生成失败
        }
        // lua生成路径
        $luaPath = $createLuaRes[1];

        $node_con = MooFile::readAll($path);
        $node_md5 = md5($node_con);
        // 存储数据库
        $nodeDao = MooDao::get('Node');

        $node_update_time = time();
        $userName = MooObj::get('User')->getUserName();
        if(!$userName) {
            return array(false, 1005); // 用户不存在,请重新登录
        }

        // 通过excelName 判断是否唯一
        $nodeObj = $nodeDao->load(array('excel_name' => $fileName, 'node_game' => $game));

        if($nodeObj) {
            // 更新
            $nodeObj->set('xml_path', $xmlPath); // xml生成路径
            $nodeObj->set('lua_path', $luaPath); // lua生成路径
            $nodeObj->set('node_md5', $node_md5);
            $nodeObj->set('node_update_time', $node_update_time);
            $nodeObj->set('node_size', $size / 1000);
            $nodeObj->set('node_update_user', $userName);
        } else {
            $node_create_time = time();
            // 初始化创建
            $nodeDao->setData('node_game', $game);
            $nodeDao->setData('excel_name', $fileName);
            $nodeDao->setData('node_activity_id', $activityNameEn);
            $nodeDao->setData('node_type', $node_type);
            $nodeDao->setData('node_md5', $node_md5);
            $nodeDao->setData('node_size', $size / 1000);
            $nodeDao->setData('node_path', $path); // 唯一标识
            $nodeDao->setData('xml_path', $xmlPath); // xml生成路径
            $nodeDao->setData('lua_path', $luaPath); // lua生成路径
            $nodeDao->setData('node_update_time', $node_update_time);
            $nodeDao->setData('node_update_user', $userName);
            $nodeDao->setData('node_create_time', $node_create_time);
            $nodeDao->setData('node_create_user', $userName);

            if(!$nodeDao->insert()) {
                return array(false, 1006); // 存储数据库失败
            }
        }
        return array(true, 1); // 移动文件失败
    }

    /**
     * 获取上传文件类型
     * @param unknown $type
     * @return string
     */
    private function getFileExt($type) {
        switch ($type) {
            case "application/vnd.ms-excel" :
                $extType = "excel";
                break;
            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" :
                $extType = "excel";
                break;
            default :
                $extType = "type_error";
                break;
        }
        return $extType;
    }
}